Completed
Push — master ( 9db7be...35eeb3 )
by Dimas
13:00
created

roles.js ➔ onlyUnique   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
1
function deleteRole(role) {
2
  console.log(getFuncName(), role);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
3
}
4
AjaxForm();
5
/**
6
 * Initialize global data source
7
 */
8
var dataSource = [];
9
jQuery(document).ready(function () {
10
  load_module(["select2"], function () {
11
    loadCSS("/assets/css/select2.min.css", function () {
12
      $.ajax({
13
        url: "",
14
        method: "post",
15
        data: {
16
          getAccess: guid(),
17
        },
18
        success: function (selectOptions) {
19
          if (selectOptions && Array.isArray(selectOptions)) {
20
            selectOptions.forEach(function (data, index) {
21
              dataSource.push({ text: data, id: data });
22
            });
23
          }
24
          //console.log(dataSource);
25
          initializeSelect2();
26
        },
27
      });
28
    });
29
  });
30
31
  $('[id^="add-select-"]').click(function (e) {
32
    e.preventDefault();
33
  });
34
});
35
36
function initializeSelect2() {
37
  jQuery('[id^="select-"],select.select2').each(function (index, value) {
38
    var t = jQuery(this);
39
    if (t.data("select2")) {
40
      console.error("select2 already initialized on " + t.attr("id"));
41
      return;
42
    }
43
    t.select2({
44
      data: dataSource.filter(onlyUnique),
45
      theme: "material",
46
      placeholder: "Select a route",
47
      allowClear: true,
48
    });
49
    var val = jQuery(this).data("key");
50
    if (val && val.length) {
51
      t.val(val).trigger("change");
52
    }
53
    //console.log(val);
54
  });
55
}
56
57
/**
58
 * Get unique array
59
 * @param {any} value
60
 * @param {any} index
61
 * @param {any[]} self
62
 * @example dataArray.filter(onlyUnique)
63
 */
64
function onlyUnique(value, index, self) {
65
  return self.indexOf(value) === index;
66
}
67